package com.htt.app.bienbaogiaothong;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.TextView;

import com.apperhand.device.android.AndroidSDKProvider;
import com.google.analytics.tracking.android.EasyTracker;
import com.htt.app.bienbaogiaothong.adapter.MainGridAdapter;
import com.htt.app.bienbaogiaothong.dialog.MyProgressDialog;
import com.htt.app.bienbaogiaothong.menu.Menu;
import com.htt.app.bienbaogiaothong.objects.Categories;
import com.htt.app.bienbaogiaothong.sqlite.DAOCategories;
import com.htt.app.bienbaogiaothong.utils.Utils;
import com.startapp.android.publish.StartAppAd;

public class MainActivity extends Activity implements OnClickListener{
	
	private GridView gridView;
	private ImageButton btnSearch;
	private ImageButton btnMenu;
	private TextView tvTitle;
	private List<Categories> data = null;
	
	private MyProgressDialog progressDialog;
	private Context ctx;
	
	private StartAppAd startAppAd = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		AndroidSDKProvider.initSDK(this);
		
		ctx = this;
		
		btnMenu = (ImageButton) findViewById(R.id.main_btnMenu);
		btnSearch = (ImageButton) findViewById(R.id.main_btnSearch);
		tvTitle = (TextView) findViewById(R.id.main_title);
		btnMenu.setOnClickListener(this);
		btnSearch.setOnClickListener(this);
		
		gridView = (GridView) findViewById(R.id.main_grid);
		
		loadData();

	}
	
	@Override
	public void onPause() {
	   super.onPause(); 
	   if(startAppAd != null) { 
	      boolean showAd = startAppAd.doHome();
	      if (showAd) {
	         startAppAd = null;
	      }
	   } 
	}
	
	@Override
	public void onBackPressed(){ 
	    if (startAppAd != null){
	       startAppAd.show();
	       startAppAd = null;
	    }
	    super.onBackPressed();
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if (startAppAd == null) { 
			   startAppAd = new StartAppAd(this);
			   startAppAd.load();
			}
	}
	
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		EasyTracker.getInstance().activityStart(this);
	}
	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		EasyTracker.getInstance().activityStop(this);
	}
	
	private void loadData() {
		new AsyncTask<Void, Void, Void>() {
			
			protected void onPreExecute() {
				progressDialog = new MyProgressDialog(ctx);
				progressDialog.startProgress("", ctx.getString(R.string.progress_load));
			};
			
			@Override
			protected Void doInBackground(Void... params) {
				// TODO Auto-generated method stub
				try {
					DAOCategories db = new DAOCategories(ctx);
					db.createDatabase();
					db.open();
					data = db.getAllCategories();
					db.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
				return null;
			}
			
			protected void onPostExecute(Void result) {
				progressDialog.stopProgress();
				
				if(Utils.haveNetworkConnection(ctx)) {
					try {
						MainGridAdapter adapter = new MainGridAdapter(ctx, data);
						gridView.setAdapter(adapter);
					} catch (Exception e) {
						e.printStackTrace();
					}
				} else {
					Utils.showDialog(ctx, ctx.getResources().getString(R.string.error_connect), 
							ctx.getResources().getString(R.string.connect_info));
				}
			};
			
		}.execute();
		
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v == btnMenu) {
			Menu.rateApp(this);
		}
		else if(v == btnSearch) {
			Menu.startSearch(this);
		}
		
	}

}
